
package com.ifcar.app.model;

import com.ifcar.app.rss.GetComments;

import android.content.Context;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Comment {    
    public static final String PARAM_F = "f";// 来源用户url，其他程序可忽略

    public static final String PARAM_U = "u";// 来源用户id

    public static final String PARAM_T = "t";// 发帖时间

    public static final String PARAM_S = "s";// 文章标题

    public static final String PARAM_B = "b";// 发帖内容

    public static final String PARAM_P = "p";// 跟帖短地址id

    public static final String PARAM_BI = "bi";// 所在版面

    public static final String PARAM_PI = "pi";// 跟帖长地址id(格式：文章id_跟帖短地址id)

    public static final String PARAM_K = "k";// 文章原文链接

    public static final String PARAM_IP = "ip";// ip地址
    
    public static final String PARAM_V = "v";// 被顶次数
    
    public static final String PARAM_NON = "NON";// 层数
    
    public static final String PARAM_EXPANDING = "expanding";// 正在展开标识
    

    public static boolean validateArticleUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        Pattern urlRegx = Pattern
                .compile(
                        "^http://([^\\s]+).163.com/\\w*/*\\d{2}/\\d{4}/\\d{2}/[0-9A-Z]{8}00[0-9]{2}([0-9A-Z]{4}|sp|rt)(_\\d{1,2}){0,1}.html$",
                        Pattern.CASE_INSENSITIVE);
        Matcher matcher = urlRegx.matcher(url);
        return matcher.matches();
    }
    
    public static Map<String, Object> expand(String boardid, String docid, String p, Context context) {
        Map<String, Object> result = new HashMap<String, Object>();
        
        Map<String, Object> map = GetComments.getOnePieceOfComment(boardid, docid, p, context);
        List<List<HashMap<String, Object>>> l1 = (List<List<HashMap<String, Object>>>)map.get(GetComments.B_TAG);
        if (l1 == null || l1.size() == 0) {
            return result;
        }
        List<HashMap<String, Object>> l2 = l1.get(0);
        if (l2 == null || l2.size() == 0) {
            return result;
        }
        final int count = l2.size();
        for (int i = count - 1; i >= 0; i--) {
            Map<String, Object> newItem = new HashMap<String, Object>();
            result.put(String.valueOf(count - i), newItem);
            Map<String, Object> item = l2.get(i);
            if (item != null) {
                newItem.put(Comment.PARAM_F, item.get(GetComments.F_TAG));
                newItem.put(Comment.PARAM_T, item.get(GetComments.TIME_TAG));
                newItem.put(Comment.PARAM_B, item.get(GetComments.B_TAG));
                newItem.put(Comment.PARAM_P, item.get(GetComments.P_TAG));
                newItem.put(Comment.PARAM_PI, item.get(GetComments.PI_TAG));
                newItem.put(Comment.PARAM_V, item.get(GetComments.VOTE_TAG));
            }
        }
        
        return result;
    }
}
